import { drawerConfig, drawerVisible } from "./drawer";
import { modalConfig, modalVisible } from "./modal";
import { searchConfig } from "./search";
import { tableData, tableConfig, tableLoading } from "./table";
import {
  tableActiveKey,
  operateType,
  queryParams,
  drawerFormState,
  searchList,
  tabPower,
  defaultSearchList,
  set_formState,
} from "./store";
import { fetchAddFriendIsMember } from "./utils";
import { isHasPower } from "@/assets/directives/tools";
import { AxiosResponse } from "axios";
export let tmp_proxy: any = null;
const defineApiMap = () => {
  return new Map([
    [0, () => gradeApi.queryVip(queryParams)],
    [1, () => gradeApi.queryPopular(queryParams)],
    [2, () => gradeApi.queryScore(queryParams)],
  ]);
};
const handleFetchListTask = async (number) => {
  const apiTask = defineApiMap();
  const api = apiTask.get(number) as () => Promise<AxiosResponse<any, any>>;
  const result: any = await api();
  if (result.code != 200) return message.error(result.message);
  tableData.value = result?.data?.list ?? [];
};
export const fetchList = async () => {
  try {
    tableData.value.length = 0;
    tableLoading.value = true;
    await handleFetchListTask(tableActiveKey.value);
  } catch (error: any) {
    message.error(error.message);
  } finally {
    tableLoading.value = false;
  }
};
export const refreshTable = async (searchParams: any = {}) => {
  initObjAttr(queryParams, searchParams);
  await fetchList();
};
export const showDrawer = () => (drawerVisible.value = true);
export const showModal = () => (modalVisible.value = true);
export const handleAction = async ({ record, type }: any) => {
  operateType.value = type;
  set_formState(type, drawerFormState, record);
};
export default defineComponent({
  setup() {
    tabPower[0] = isHasPower("gradeApi.queryVip");
    tabPower[1] = isHasPower("gradeApi.queryPopular");
    tabPower[2] = isHasPower("gradeApi.queryScore");
    tableActiveKey.value = tabPower[0]
      ? 0
      : tabPower[1]
      ? 1
      : tabPower[2]
      ? 2
      : Infinity;
    searchList.value = defaultSearchList[tableActiveKey.value] ?? [];
    const { proxy } = reactive<any>(getCurrentInstance());
    tmp_proxy = proxy;
    onActivated(() => {
      fetchList();
      fetchAddFriendIsMember();
    });
    const defineSearch = searchConfig();
    const defineTable = tableConfig();
    const defineDrawer = drawerConfig();
    const defineModal = modalConfig();
    return () => (
      <s-design
        defineSearch={defineSearch}
        defineTable={defineTable}
        defineDrawer={defineDrawer}
        defineModal={defineModal}
      />
    );
  },
});
